﻿
@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
班级列表页
@*添加按钮*@
<div class="container">
    <div class="row">
        <div class="col-1">
            <button id="addClassEntity" class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#addClassModel" aria-controls="offcanvasWithBothOptions">添加</button>
        </div>
        <div class="col-3">
            <div class="input-group mb-3">
                <input type="text" class="form-control" id="searchWords" placeholder="班级名称" aria-label="Recipient's username" aria-describedby="searchClass">
                <button class="btn btn-outline-secondary" type="button" id="searchClass">搜索</button>
            </div>
        </div>
        <div class="col-1">
            
        </div>
        <div class="col-3">
            <div class="input-group input-group mb-2">
                <label class="input-group-text">年级:</label>
                <select id="findGradeId" class="form-select form-select" aria-label="">
                    <option value="0">全部</option>
                </select>
            </div>
        </div>
        <div class="col-3">
            <div class="input-group input-group mb-2">
                <label class="input-group-text">专业:</label>
                <select id="findDepartmentId" class="form-select form-select" aria-label="">
                    <option value="0">全部</option>
                </select>
            </div>
        </div>
    </div>
</div>



@*表格*@
<table id="classTable" class="table table-bordered table-hover">
    <thead>
        <tr>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>



@*添加对话框*@
<div class="offcanvas offcanvas-bottom" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="addClassModel" aria-labelledby="offcanvasScrollingLabel">
    <div class="offcanvas-header">
        <h5 class="offcanvas-title" id="offcanvasScrollingLabel">添加班级</h5>
        <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
        <div class="container text-center">
            <div class="row">
               
                    @*第一列*@
                    <div class="col-sm-12 col-md-6 col-lg-3">

                        <div class="input-group input-group-sm mb-2">
                            <label class="input-group-text">年级:</label>
                            <select id="gradeId" class="form-select form-select-sm" aria-label="">
                                <option value="0">请选择</option>
                            </select>
                        </div>

                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon2">专业</span>
                            <select id="departmentId" class="form-select form-select-sm" aria-label="">
                                <option value="0">请选择</option>
                            </select>
                        </div>

                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon3">班级名称</span>
                            <input type="text" id="className" class="form-control" aria-describedby="basic-addon1">
                        </div>

                    </div>
                    @*第二列*@
                    <div class="col-sm-12 col-md-6 col-lg-3">
                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon4">班主任</span>
                            <input type="text" id="userName" class="form-control" aria-describedby="basic-addon1">
                        </div>


                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon5">教室</span>
                        <button class="btn btn-outline-secondary" type="button" id="button-addon2" data-bs-toggle="modal" data-bs-target="#findClassRoom">选择</button>
                        <select id="classRoomId" class="form-select form-select-sm" aria-label="" disabled>
                                <option value="0">请选择</option>
                            </select>
                        
                        </div>

                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon6">班长</span>
                            <input type="text" class="form-control" id="head" aria-describedby="basic-addon1">
                        </div>
                    </div>
                    @*第三列*@
                    <div class="col-sm-12 col-md-6 col-lg-3">
                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon7">学委</span>
                            <input type="text" class="form-control" id="learn" aria-describedby="basic-addon1">
                        </div>
                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon8">团支书</span>
                            <input type="text" class="form-control" id="secretary" aria-describedby="basic-addon1">
                        </div>
                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon9">体委</span>
                            <input type="text" class="form-control" id="sport" aria-describedby="basic-addon1">
                        </div>
                    </div>
                    @*第四列*@
                    <div class="col-sm-12 col-md-6 col-lg-3">

                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon10">劳动委员</span>
                            <input type="text" class="form-control" id="work" aria-describedby="basic-addon1">
                        </div>
                        <div class="input-group input-group-sm mb-2">
                            <span class="input-group-text" id="basic-addon11">电教委员</span>
                            <input type="text" class="form-control" id="electron" aria-describedby="basic-addon1">
                        </div>

                        <div class="modal-footer form-control-sm">
                            <button type="button" class="btn btn-primary btn-sm" id="submitAdd">保存</button>
                            <button type="button" class="btn btn-secondary btn-sm" id="clearClassEntity" data-bs-dismiss="modal">清空</button>
                        </div>

                    </div>

            </div>
        </div>
    </div>
</div>


@*教室详情*@
<div class="modal fade " id="findClassRoom" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="staticBackdropLabel">教室使用情况</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <table id="classRoomDetails" class="table">
                    <thead>
                        <tr>
                            <th scope="col">教室id</th>
                            <th scope="col">教室名称</th>
                            <th scope="col">容量</th>
                            <th scope="col">已使用</th>
                            <th scope="col">选择</th>
                        </tr>
                    </thead>
                    <tbody>
                        
                    </tbody>
                </table>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
</div>


@*消息框*@
<div class="toast-container position-fixed top-0 start-50 p-3">
    <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
            <strong class="me-auto">消息</strong>
            <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        <div class="toast-body" id="message">
            
        </div>
    </div>
</div>



@*编辑对话框*@
<div class="offcanvas offcanvas-bottom" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="editClassEntity" aria-labelledby="offcanvasScrollingLabel">
    <div class="offcanvas-header">
        <h5 class="offcanvas-title" id="">编辑班级</h5>
        <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
        <div class="container text-center">
            <div class="row">

                @*第一列*@
                <div class="col-sm-12 col-md-6 col-lg-3">

                    

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon1">班级id</span>
                        <input type="text" id="newClassEntityId" class="form-control" aria-describedby="basic2-addon1">
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon2">班级名称</span>
                        <input type="text" id="newClassName" class="form-control" aria-describedby="basic2-addon2">
                    </div>

                </div>
                @*第二列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon3">班主任</span>
                        <input type="text" id="newUserName" class="form-control" aria-describedby="basic2-addon3">
                    </div>


                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon4">教室</span>
                        <select id="newClassRoomId" class="form-select form-select-sm" aria-label="" disabled>
                            <option value="0">请选择</option>
                        </select>
                        <button class="btn btn-outline-secondary" type="button" id="basic2-addon4" data-bs-toggle="modal" data-bs-target="#findClassRoom2">选择</button>
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon5">班长</span>
                        <input type="text" class="form-control" id="NewHead" aria-describedby="basic2-addon5">
                    </div>
                </div>
                @*第三列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon6">学委</span>
                        <input type="text" class="form-control" id="newLearn" aria-describedby="basic2-addon6">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon7">团支书</span>
                        <input type="text" class="form-control" id="newSecretary" aria-describedby="basic2-addon7">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon8">体委</span>
                        <input type="text" class="form-control" id="newSport" aria-describedby="basic2-addon8">
                    </div>
                </div>
                @*第四列*@
                <div class="col-sm-12 col-md-6 col-lg-3">

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon9">劳动委员</span>
                        <input type="text" class="form-control" id="newWork" aria-describedby="basic2-addon9">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic2-addon10">电教委员</span>
                        <input type="text" class="form-control" id="newElectron" aria-describedby="basic2-addon10">
                    </div>

                    <div class="modal-footer form-control-sm">
                        <button type="button" class="btn btn-primary btn-sm" id="submitEdit">保存</button>
                    </div>

                </div>

            </div>
        </div>
    </div>
</div>


@*编辑中教室详情*@
<div class="modal fade " id="findClassRoom2" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="">教室使用情况</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <table id="classRoomDetails2" class="table">
                    <thead>
                        <tr>
                            <th scope="col">教室id</th>
                            <th scope="col">教室名称</th>
                            <th scope="col">容量</th>
                            <th scope="col">已使用</th>
                            <th scope="col">选择</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal">确定</button>
            </div>
        </div>
    </div>
</div>


@*确定删除对话框*@
<div class="modal fade" id="sureDel" tabindex="-1" data-bs-backdrop="static" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">删除班级</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>确认删除吗?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal" id="submitDel">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        loadpage()
        let g_rows
        let user = JSON.parse(sessionStorage.getItem('user'))

        let deplist
        let gradelist
        let classRoomlist

        getClassRoom()
        getSelectData()
        rendSelects()


        function loadpage() {
            let s = sessionStorage.getItem('classlist')

            if (s == [] || s == "" || s == null) {
                $.ajax({
                    url: '/ClassEntity/GetClassList',
                    type: 'get',
                    success: function (data) {
                        parseData(data)
                    },
                    error: function (err) {
                    }
                })
            } else {
                parseStorage(s)
            }

        }

        //解析请求数据并渲染到页面
        function parseData(data) {
            $('#classTable thead tr').empty()
            $('#classTable tbody tr').empty()
            let jsd = JSON.stringify(data)

            let head = JSON.parse(data.attr)
            head.push('操作')
            sessionStorage.setItem('classlist_head', JSON.stringify(head))

            //渲染表头
            for (index in head) {
                $('#classTable thead tr').append('<th>' + head[index] + '</th>')
            }

            let rows = JSON.parse(data.row)
            g_rows = rows
            sessionStorage.setItem('classlist', JSON.stringify(rows))

            //渲染表格
            $(rows).each(function (index, ele) {
                $('#classTable tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#classTable tbody tr:last').append('<td>' + ele[key] + '</>')
                }

                $('#classTable tbody tr:last').append('<td>' +
                    '<button class="editBtn" onclick="edit(this)" data-bs-toggle="offcanvas" data-bs-target="#editClassEntity">编辑</button>' +
                    '<button class="delBtn" onclick="del(this)" data-bs-toggle="modal" data-bs-target="#sureDel">删除</button>' +
                    '</td>')
                $('#classTable tbody tr td:last').attr({ 'classid': ele.ClassEntityId })
            })

            $('.delBtn').addClass("btn btn-danger btn-sm")
            $('.editBtn').addClass("btn btn-primary btn-sm")
        }



        //解析缓存并加载到表格
        function parseStorage(data) {
            $('#classTable thead tr').empty()
            $('#classTable tbody tr').empty()
            let headStorage = sessionStorage.getItem('classlist_head')
            let head = JSON.parse(headStorage)
            //渲染表头
            for (index in head) {
                $('#classTable thead tr').append('<th>' + head[index] + '</th>')
            }

            //渲染表格
            let rows = JSON.parse(data)
            $(rows).each(function (index, ele) {
                $('#classTable tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#classTable tbody tr:last').append('<td>' + ele[key] + '</>')
                }
                $('#classTable tbody tr:last').append('<td>' +
                    '<button class="editBtn" data-bs-toggle="offcanvas" data-bs-target="#editClassEntity">编辑</button>' +
                    '<button class="delBtn" data-bs-toggle="modal" data-bs-target="#sureDel">删除</button>' +
                    '</td>')
                $('#classTable tbody tr td:last').attr({ 'classid': ele.ClassEntityId })
            })

            $('.delBtn').addClass("btn btn-danger btn-sm")
            $('.editBtn').addClass("btn btn-primary btn-sm")
        }


        //刷新数据
        function refreshData() {
            $('#classTable thead tr').empty()
            $('#classTable tbody tr').empty()
            if ($('#findGradeId').val() == 0 && $('#findDepartmentId').val()==0) {
                $.ajax({
                    url: '/ClassEntity/GetClassList',
                    type: 'get',
                    success: function (data) {
                        parseData(data)
                    },
                    error: function (err) {
                    }
                })
                return
            }

            if ($('#findGradeId').val() != 0 && $('#findDepartmentId').val() == 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByGradeId',
                    type: 'post',
                    data: {
                        GradeId: $('#findGradeId').val(),
                        DepartmentId: $('#findDepartmentId').val()
                    },
                    success: function (data) {
                        parseData(data)
                    },
                    error: function (err) {
                    }
                })
                return
            }

            if ($('#findGradeId').val() == 0 && $('#findDepartmentId').val() != 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByDepartmentId',
                    type: 'post',
                    data: {
                        GradeId: $('#findGradeId').val(),
                        DepartmentId: $('#findDepartmentId').val()
                    },
                    success: function (data) {
                        parseData(data)
                    },
                    error: function (err) {
                    }
                })
                return
            }

            if ($('#findGradeId').val() != 0 && $('#findDepartmentId').val() != 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByGradeIdAndDepartmentId',
                    type: 'post',
                    data: {
                        GradeId: $('#findGradeId').val(),
                        DepartmentId: $('#findDepartmentId').val()
                    },
                    success: function (data) {
                        parseData(data)
                    },
                    error: function (err) {
                    }
                })
                return
            }
            
        }


        //获取下拉框数据
        function getSelectData() {
            let gradeStor = JSON.parse(sessionStorage.getItem('gradelist'))
            let depStor = JSON.parse(sessionStorage.getItem('deplist'))
            //let classRoomStor = JSON.parse(sessionStorage.getItem('classRoomlist'))
            if (gradeStor == null || depStor == null ) {
                initSelectData()
            }
            else {
                gradelist = gradeStor
                deplist = depStor
                //classRoomlist = classRoomStor
            }

        }


        //初始化下拉框数据 存缓存
        function initSelectData() {
            $.ajax({
                url: '/Grade/GetGradeList',
                type: 'get',
                success: function (data) {
                    sessionStorage.setItem('gradelist', data.row)
                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('gradelist_head', JSON.stringify(head))
                    gradelist = JSON.parse(data["row"])
                },
                error: function (err) {
                }
            })

            $.ajax({
                url: '/Department/GetDepList',
                type: 'get',
                success: function (data) {
                    sessionStorage.setItem('deplist', data.row)

                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('deplist_head', JSON.stringify(head))
                    deplist = JSON.parse(data['row'])
                },
                error: function (err) {
                }
            })

        }

        function getClassRoom()
        {
            $.ajax({
                url: '/ClassRoom/GetClassRoomList',
                type: 'get',
                success: function (data) {
                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('classRoomlist_head', JSON.stringify(head))
                    classRoomlist = JSON.parse(data['row'])
                    rendClassRoom(classRoomlist)
                    rendClassRoom2(classRoomlist)
                },
                error: function (err) {
                }
            })
        }
        //渲染教室详情
        function rendClassRoom(data) {
            
            let details = []
            $(data).each(function (index, ele) {
                let item = {}
                item.ClassRoomId = ele.ClassRoomId
                item.RoomName = ele.RoomName
                item.Capacity = ele.Capacity
                item.IsBeUsed = ele.ClassName == null ? '否' : '是'
                item.Checked = ele.ClassName == null ? false : true
                details.push(item)
            })

            $('#classRoomDetails tbody').empty()
            $(details).each(function (index, ele) {

                $('#classRoomDetails tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#classRoomDetails tbody tr:last').append('<td>' + ele[key] + '</>')

                }

                $('#classRoomDetails tbody tr:last').append('<td>' +
                    '<input class="form-check-input classCheck" onclick="checkOne(this)" type="radio" name="flexRadioDefault" id="" value="">' +
                    '</td>')

                $('#classRoomDetails tbody tr td:last>input').attr({ 'disabled': ele.Checked, 'id': ele.ClassRoomId, 'value': ele.ClassRoomId })
            })

        }

        //渲染"添加"对话框中下拉框
        function rendSelects() {

            $('#gradeId').empty()
            $('#gradeId').append("<option value='0'>请选择</option>")
            $(gradelist).each(function (index, ele) {
                $('#gradeId').append("<option value=''></option>")
                $('#gradeId option:last').val(ele.GradeId)
                $('#gradeId option:last').text(ele.GradeName)
            })

            $('#departmentId').empty()
            $('#departmentId').append("<option value='0'>请选择</option>")
            $(deplist).each(function (index, ele) {
                $('#departmentId').append("<option value=''></option>")
                $('#departmentId option:last').val(ele.DepartmentId)
                $('#departmentId option:last').text(ele.DepName)
            })
            

            $('#classRoomId').empty()
            $('#classRoomId').append("<option value='0'>请选择</option>")
            $(classRoomlist).each(function (index, ele) {
                $('#classRoomId').append("<option value=''></option>")
                $('#classRoomId option:last').val(ele.ClassRoomId)
                $('#classRoomId option:last').text(ele.RoomName)
            })

            //渲染编辑对话框中的下拉框
            $('#newClassRoomId').empty()
            $('#newClassRoomId').append("<option value='0'>请选择</option>")
            $(classRoomlist).each(function (index, ele) {
                $('#newClassRoomId').append("<option value=''></option>")
                $('#newClassRoomId option:last').val(ele.ClassRoomId)
                $('#newClassRoomId option:last').text(ele.RoomName)
            })


            $('#findGradeId').empty()
            $('#findGradeId').append("<option value='0'>全部</option>")
            $(gradelist).each(function (index, ele) {
                $('#findGradeId').append("<option value=''></option>")
                $('#findGradeId option:last').val(ele.GradeId)
                $('#findGradeId option:last').text(ele.GradeName)
            })


            $('#findDepartmentId').empty()
            $('#findDepartmentId').append("<option value='0'>全部</option>")
            $(deplist).each(function (index, ele) {
                $('#findDepartmentId').append("<option value=''></option>")
                $('#findDepartmentId option:last').val(ele.DepartmentId)
                $('#findDepartmentId option:last').text(ele.DepName)
            })

        }

        $('#addClassEntity').click(function () {
            rendSelects()
        })


      


        //选择教室
       
        function checkOne(e)
        {
            $('#classRoomId').val($(e).val())
        }



        const toastLiveExample = document.getElementById('liveToast')
        const toast = new bootstrap.Toast(toastLiveExample)
        //提交添加
        $('#submitAdd').click(function () {
            let gradeId = $('#gradeId').val()
            let departmentId = $('#departmentId').val()
            let className = $('#className').val()
            let userName = $('#userName').val()
            let classRoomId = $('#classRoomId').val()
            let head = $('#head').val()
            let learn = $('#learn').val()
            let secretary = $('#secretary').val()
            let sport = $('#sport').val()
            let work = $('#work').val()
            let electron = $('#electron').val()
            if (className == '' || className == null) {
                
                $('#message').text('班级名称不能为空!')
                toast.show()
                return
            }

            if (gradeId <= 0 || departmentId <= 0 || classRoomId<=0) {
                $('#message').text('年级，专业,教室必选!')
                toast.show()
                return
            }
            $.ajax({
                url: '/ClassEntity/AddClassEntity',
                type: 'post',
                data: {
                    GradeId: gradeId,
                    DepartmentId: departmentId,
                    ClassName: className,
                    UserName: userName,
                    ClassRoomId: classRoomId,
                    Head: head,
                    Learn: learn,
                    Secretary: secretary,
                    Sport: sport,
                    Work: work,
                    Electron: electron,
                },
                success: function (data) {
                    if (data == '1') {
                        $('#message').text('添加成功!')
                        toast.show()
                        refreshData()
                        getClassRoom()
                    }
                    if(data=='-1'){
                        $('#message').text('班级名称已存在!')
                        toast.show()
                    }
                },
                error: function (err) {
                }
            })
        })

        //清空按钮
        $('#clearClassEntity').click(function(){
            $('#userName').val('')
            $('#classRoomId').val(0)
            $('#head').val('')
            $('#learn').val('')
            $('#secretary').val('')
            $('#sport').val('')
            $('#work').val('')
            $('#electron').val('')
        })


        //渲染编辑中教室详情
        function rendClassRoom2() {
            let details = []
            $(classRoomlist).each(function (index, ele) {
                let item = {}
                item.ClassRoomId = ele.ClassRoomId
                item.RoomName = ele.RoomName
                item.Capacity = ele.Capacity
                item.IsBeUsed = ele.ClassName == null ? '否' : '是'
                item.Checked = ele.ClassName == null ? false : true
                details.push(item)
            })

            $('#classRoomDetails2 tbody').empty()
            $(details).each(function (index, ele) {

                $('#classRoomDetails2 tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#classRoomDetails2 tbody tr:last').append('<td>' + ele[key] + '</>')

                }

                $('#classRoomDetails2 tbody tr:last').append('<td>' +
                    '<input class="form-check-input classCheck2" onclick="checkOne2(this)" type="radio" name="flexRadioDefault" id="" value="">' +
                    '</td>')

                $('#classRoomDetails2 tbody tr td:last>input').attr({ 'disabled': ele.Checked, 'id': ele.ClassRoomId, 'value': ele.ClassRoomId })
            })

        }


        
        //第一次加载页面--“编辑”按钮响应事件
        function edit(e) {
            rendSelects()
            let classId = $(e).parent().attr('classid')
            let classEntity = g_rows.filter(function (d) {
                return d.ClassEntityId == classId
            })
            $('#newClassEntityId').val(classEntity[0].ClassEntityId)
            $('#newClassEntityId').attr({ 'disabled': 'disabled' })
            $('#newClassName').val(classEntity[0].ClassName)
            $('#newUserName').val(classEntity[0].UserName)
            $('#newClassRoomId').val(classEntity[0].ClassRoomId)
            $('#NewHead').val(classEntity[0].Head)
            $('#newLearn').val(classEntity[0].Learn)
            $('#newSecretary').val(classEntity[0].Secretary)
            $('#newSport').val(classEntity[0].Sport)
            $('#newWork').val(classEntity[0].Work)
            $('#newElectron').val(classEntity[0].Electron)
        }

        //使用缓存--“编辑”按钮响应事件
        $('.editBtn').each(function (index) {
            rendSelects()
            $(this).click(function () {
                let rows = JSON.parse(sessionStorage.getItem('classlist'))
                let classEntity = rows.filter(function (r, ind) {
                    return index == ind
                })
                $('#newClassEntityId').val(classEntity[0].ClassEntityId)
                $('#newClassEntityId').attr({ 'disabled': 'disabled' })
                $('#newClassName').val(classEntity[0].ClassName)
                $('#newUserName').val(classEntity[0].UserName)
                $('#newClassRoomId').val(classEntity[0].ClassRoomId)
                $('#NewHead').val(classEntity[0].Head)
                $('#newLearn').val(classEntity[0].Learn)
                $('#newSecretary').val(classEntity[0].Secretary)
                $('#newSport').val(classEntity[0].Sport)
                $('#newWork').val(classEntity[0].Work)
                $('#newElectron').val(classEntity[0].Electron)
            })
        })

        function checkOne2(e) {
            console.log(e)
            $('#newClassRoomId').val($(e).val())
        }
       
        //编辑对话框中的“保存”按钮点击事件响应
        $('#submitEdit').click(function (index) {
            let ClassEntityId = $('#newClassEntityId').val()
            let ClassName = $('#newClassName').val()
            let UserName = $('#newUserName').val()
            let ClassRoomId = $('#newClassRoomId').val()
            let Head = $('#NewHead').val()
            let Learn = $('#newLearn').val()
            let Secretary = $('#newSecretary').val()
            let Sport = $('#newSport').val()
            let Work = $('#newWork').val()
            let Electron = $('#newElectron').val()

            if (ClassName == '') {
                //提示名称不能为空
                $('#message').text('班级名称不能为空!')
                toast.show()
                return
            }

            $.ajax({
                url: '/ClassEntity/EditClassEntity',
                type: 'post',
                data: {
                    ClassEntityId,
                    ClassName,
                    UserName,
                    ClassRoomId,
                    Head,
                    Learn,
                    Secretary,
                    Sport,
                    Work,
                    Electron,
                },
                success: function (data) {
                    if (data == 1) {
                        $('#message').text('修改成功!')
                        toast.show()
                    }
                    else if (data == 0) {
                        $('#message').text('班级名称已存在!')
                        toast.show()
                    } else if (data == -1) {
                        $('#message').text('班级名称不能为空!')
                        toast.show()
                    }
                    //刷新表格
                    refreshData()
                    //关闭对话框
                    // ...
                },
                error: {

                }
            })
        })




        //第一次加载页面--"删除"按钮响应事件
        let classEntityId
        function del(e) {
            classEntityId = $(e).parent().attr('classid')
        }

        //缓存的数据--"删除"按钮点击事件响应
        $('.delBtn').each(function (index) {
            $(this).click(function () {
                let stor = sessionStorage.getItem('classlist')

                let rows = JSON.parse(stor)
                let tr = $(rows).filter(function (ind, ele) {
                    return ind == index
                })
                classEntityId = tr[0].ClassEntityId
            })
        })


        //"删除"对话框--"确定"按钮响应事件
        $('#submitDel').click(function () {
            let delId = classEntityId;
            if (delId <= 0) {
                return
            }

            $.ajax({
                url: '/ClassEntity/DelClassEntity',
                type: 'post',
                data: {
                    ClassEntityId: delId
                },
                success: function (data) {
                    if (data == 1) {
                        $('#message').text('删除成功!')
                        toast.show()
                        getClassRoom()
                    }
                    else {
                        $('#message').text('删除失败!')
                        toast.show()
                    }
                    //刷新表格
                    refreshData()
                    //关闭对话框
                    // ...
                },
                error: {

                }
            })
        })


        $('#findGradeId').change(function(){
            let findGradeId = $('#findGradeId').val()
            let findDepartmentId = $('#findDepartmentId').val()
            findClassEntity(findGradeId,findDepartmentId)
        })

        $('#findDepartmentId').change(function () {
            let findDepartmentId = $('#findDepartmentId').val()
            let findGradeId = $('#findGradeId').val()
            findClassEntity(findGradeId, findDepartmentId)
        })


        function rendTable(data)
        {
            $('#classTable tbody').empty()
            //渲染表格
            if(data==null){
                return 
            }
            $(data).each(function (index, ele) {
                $('#classTable tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#classTable tbody tr:last').append('<td>' + ele[key] + '</>')
                }

                $('#classTable tbody tr:last').append('<td>' +
                    '<button class="editBtn" onclick="edit(this)" data-bs-toggle="offcanvas" data-bs-target="#editClassEntity">编辑</button>' +
                    '<button class="delBtn" onclick="del(this)" data-bs-toggle="modal" data-bs-target="#sureDel">删除</button>' +
                    '</td>')
                $('#classTable tbody tr td:last').attr({ 'classid': ele.ClassEntityId })
            })

            $('.delBtn').addClass("btn btn-danger btn-sm")
            $('.editBtn').addClass("btn btn-primary btn-sm")
        }

        function findClassEntity(gradeId,departmentId)
        {
            if (gradeId == 0 && departmentId == 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassList',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendTable(res)
                    },
                    error: {

                    }
                })
                return
            }
            if(gradeId!=0&&departmentId==0){
                $.ajax({
                    url: '/ClassEntity/GetClassByGradeId',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendTable(res)
                    },
                    error: {

                    }
                })
                return
            }

            if (gradeId == 0 && departmentId != 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByDepartmentId',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendTable(res)
                    },
                    error: {

                    }
                })
                return
            }

            if (gradeId != 0 && departmentId != 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByGradeIdAndDepartmentId',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendTable(res)
                    },
                    error: function (err) {
                    }
                })
                return
            }
        }


        //搜索按钮
        $('#searchClass').click(function(){
            let key = $('#searchWords').val()
            if(key==null||key=='')
            {
                return 
            }
            $.ajax({
                url: '/ClassEntity/SearchClassEntity',
                type: 'post',
                data: {
                    ClassName: key
                },
                success: function (data) {
                    let res = JSON.parse(data.row)
                    rendTable(res)
                },
                error: {

                }
            })
        })
    </script>

}